<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" class="formWidth" label-width="90px">
      <!-- <el-form-item label="关联soldier_base_info" prop="soldierId">
        <el-input
          v-model="queryParams.soldierId"
          placeholder="请输入关联soldier_base_info"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="军休类别" prop="retireType">
        <el-select v-model="queryParams.retireType" placeholder="请选择军休类别" clearable size="small">
          <el-option
            v-for="dict in retireTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入伍时间" prop="enlistedDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.enlistedDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择入伍时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="离休退休时间" prop="retirementDate" label-width="100px">
        <el-date-picker clearable size="small"
          v-model="queryParams.retirementDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择离休退休时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="离休退休证件号码" prop="retirementCertificateCode" label-width="130px">
        <el-input
          v-model="queryParams.retirementCertificateCode"
          placeholder="请输入离休退休证件号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安置日期" prop="arrangeDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.arrangeDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择安置日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="安置地" prop="arrangePlace">
        <el-input
          v-model="queryParams.arrangePlace"
          placeholder="请输入安置地 省市县/区"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安置单位名称" prop="arrangeUnit" label-width="120px">
        <el-input
          v-model="queryParams.arrangeUnit"
          placeholder="请输入安置单位名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安置单位性质" prop="arrangeUnitType" label-width="120px">
        <el-input
          v-model="queryParams.arrangeUnitType"
          placeholder="请输入安置单位性质"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务管理机构名称" prop="organizationName" label-width="140px">
        <el-input
          v-model="queryParams.organizationName"
          placeholder="请输入服务管理机构名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['archive_info:retired_cadre:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['archive_info:retired_cadre:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['archive_info:retired_cadre:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['archive_info:retired_cadre:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="retired_cadreList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" :show-overflow-tooltip="true"/>
      <el-table-column label="姓名" align="center" prop="soldierName" :show-overflow-tooltip="true"/>
      <el-table-column label="身份证号" :show-overflow-tooltip="true" align="center" prop="idCard"  width="190" :formatter="idNumberFormat"/>
      <el-table-column label="入伍时间" align="center" prop="enlistedDate" width="180" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.enlistedDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="离休退休时间" align="center" prop="retirementDate" width="180" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.retirementDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务管理机构名称" align="center" prop="id" :show-overflow-tooltip="true"/>
      <!-- <el-table-column label="关联soldier_base_info" align="center" prop="soldierId" /> -->
      <el-table-column label="军休类别" align="center" prop="retireType" :formatter="retireTypeFormat" :show-overflow-tooltip="true"/>
      <el-table-column label="离休退休证件号码" align="center" prop="retirementCertificateCode" :show-overflow-tooltip="true"/>
      <el-table-column label="安置日期" align="center" prop="arrangeDate" width="180" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.arrangeDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="安置地 省市县/区" align="center" prop="arrangePlace" :show-overflow-tooltip="true"/>
      <el-table-column label="安置单位名称" align="center" prop="arrangeUnit" :show-overflow-tooltip="true"/>
      <el-table-column label="安置单位性质" align="center" prop="arrangeUnitType" :show-overflow-tooltip="true"/>
      <el-table-column label="服务管理机构名称" align="center" prop="organizationName" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['archive_info:retired_cadre:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['archive_info:retired_cadre:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改军队离休退休干部和退休士官信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" class="formWidth" label-width="130px">
        <!-- <el-form-item label="关联soldier_base_info" prop="soldierId">
          <el-input v-model="form.soldierId" placeholder="请输入关联soldier_base_info" />
        </el-form-item> -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="军休类别" prop="retireType">
              <el-select v-model="form.retireType" placeholder="请选择军休类别">
                <el-option
                  v-for="dict in retireTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入伍时间" prop="enlistedDate">
              <el-date-picker clearable size="small"
                v-model="form.enlistedDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择入伍时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="离休退休时间" prop="retirementDate">
              <el-date-picker clearable size="small"
                v-model="form.retirementDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择离休退休时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="离休退休证件号码" prop="retirementCertificateCode">
              <el-input v-model="form.retirementCertificateCode" placeholder="请输入离休退休证件号码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="安置日期" prop="arrangeDate">
              <el-date-picker clearable size="small"
                v-model="form.arrangeDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择安置日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="安置地" prop="arrangePlace">
              <el-input v-model="form.arrangePlace" placeholder="请输入安置地 省市县/区" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="安置单位名称" prop="arrangeUnit">
              <el-input v-model="form.arrangeUnit" placeholder="请输入安置单位名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="安置单位性质" prop="arrangeUnitType">
              <el-input v-model="form.arrangeUnitType" placeholder="请输入安置单位性质" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="服务管理机构名称" prop="organizationName">
              <el-input v-model="form.organizationName" placeholder="请输入服务管理机构名称" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRetired_cadre, getRetired_cadre, delRetired_cadre, addRetired_cadre, updateRetired_cadre, exportRetired_cadre } from "@/api/archive_info/retired_cadre";

export default {
  name: "Retired_cadre",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 军队离休退休干部和退休士官信息表格数据
      retired_cadreList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 军休类别字典
      retireTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        soldierId: null,
        retireType: null,
        enlistedDate: null,
        retirementDate: null,
        retirementCertificateCode: null,
        arrangeDate: null,
        arrangePlace: null,
        arrangeUnit: null,
        arrangeUnitType: null,
        organizationName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // soldierId: [
        //   { required: true, message: "关联soldier_base_info不能为空", trigger: "blur" }
        // ],
        retireType: [
          { required: true, message: "军休类别不能为空", trigger: "change" }
        ],
        enlistedDate: [
          { required: true, message: "入伍时间不能为空", trigger: "blur" }
        ],
        retirementDate: [
          { required: true, message: "离休退休时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("retire_type").then(response => {
      this.retireTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询军队离休退休干部和退休士官信息列表 */
    getList() {
      this.loading = true;
      listRetired_cadre(this.queryParams).then(response => {
        this.retired_cadreList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 军休类别字典翻译
    retireTypeFormat(row, column) {
      return this.selectDictLabel(this.retireTypeOptions, row.retireType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        soldierId: null,
        retireType: null,
        enlistedDate: null,
        retirementDate: null,
        retirementCertificate: null,
        retirementCertificateCode: null,
        arrangeDate: null,
        arrangePlace: null,
        arrangeUnit: null,
        arrangeUnitType: null,
        organizationName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加军队离休退休干部和退休士官信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRetired_cadre(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改军队离休退休干部和退休士官信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRetired_cadre(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRetired_cadre(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除军队离休退休干部和退休士官信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRetired_cadre(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有军队离休退休干部和退休士官信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportRetired_cadre(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    idNumberFormat(row,column){
      return row.idNumber.replace(/^(.{6})(?:\w+)(.{4})$/, "\$1********\$2")
    },
  }
};
</script>
